let interval = null
let interstitialAd = null
let rewardedVideoAd = null
const interstitialAdUnit = 'adunit-8eeb07a81170a237'
const initRewardVideoAdUnit = 'adunit-8b00d591e977d0b0'
import weappQRcode from 'weapp-qrcode'
Page({
  data: {
    text: '',
    mfcs:1,
    cs:0
  },
  onShareAppMessage(){
    return {
      title:'扫码宝典',
      path:'/pages/home/index',
      imageUrl:'/logo.jpg'
    }
  },
  onLoad() {
    this.initInterstitialAd(interstitialAdUnit)
    this.initRewardVideoAd(initRewardVideoAdUnit)
    setTimeout(() => {
      this.setInter()
    }, 10000)
  },
  onUnload() {
    if (interval) {
      clearInterval(interval)
    }
  },
  adError(e) {
    console.log(e)
  },
  setInter() {
    if (interval) {
      clearInterval(interval)
    }
    interval = setInterval(() => {
      interstitialAd.show()
    }, 120000)
  },
  initInterstitialAd(adUnitId) {
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId
      })
      interstitialAd.onLoad(() => {
        console.log('onLoad event emit')
      })
      interstitialAd.onError((err) => {
        console.log('onError event emit', err)
      })
      interstitialAd.onClose((res) => {
        console.log('onClose event emit', res)
      })
      if (interstitialAd) {
        interstitialAd.show().catch((err) => {
          console.error(err)
        })
      }
    }
  },
  initRewardVideoAd(adUnitId) {
    if (wx.createRewardedVideoAd) {
      rewardedVideoAd = wx.createRewardedVideoAd({
        adUnitId
      })
      rewardedVideoAd.onLoad(() => {
        console.log('onLoad event emit')
      })
      rewardedVideoAd.onError((err) => {
        console.log('onError event emit', err)
      })
      rewardedVideoAd.onClose((res) => {
        console.log('onClose event emit', res)
      })
    }
  },
  handleInput(e) {
    const text = e.detail.value
    this.setData({
      text
    })
  },
  // 确认生成二维码
  handleConfirm() {
    const {
      text
    } = this.data
    new weappQRcode({
      width: 150,
      height: 150,
      canvasId: 'myQRcode',
      text
    })
  },
  // 扫码识别
  handleScan() {
    wx.scanCode({
      success: ({
        result: text
      }) => {
        wx.showToast({
          title: '扫码成功',
          type: 'success'
        })
        this.setData({
          text
        })
      }
    })
  },
  // 二维码保存到手机
  handleSave() {
    wx.canvasToTempFilePath({
      canvasId: 'myQRcode',
      success: ({
        tempFilePath: filePath
      }) => {
        wx.saveImageToPhotosAlbum({
          filePath,
          success: () => {
            wx.showToast({
              title: '保存成功',
              type: 'success'
            })
            if(this.data.cs >= this.data.mfcs){
              if(rewardedVideoAd !== null){
                rewardedVideoAd.show()
              }
            }else{
              this.setData({
                cs : this.data.cs + 1
              })
            }
          }
        })
      }
    })
  }
})